package com.ebring.shareEducation.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class VipUtil {
    /**
     * 根据起始日期，支付金额，价格，返回Vip到期时间
     * @param startTime 起始日期
     * @param money 金额
     * @param price 价格
     * @return
     */
    public static String countVipEndDate(String startTime,String money,String price){
        int amount = (int) (Double.parseDouble(money)/Double.parseDouble(price));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.MONTH,amount);
        String endDate = sdf.format(calendar.getTime());
        return endDate;
    }

    /**
     *  计算vip升级需要的金额
     * @param price 超级会员与普通会员单月价格差
     * @param endTime vip结束日期
     * @return
     */
    public static Double countVipUpLevelPrice(Double price,String endTime){
        double d = 0.0;
        double p = price;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = new Date();
        Date endDate = null;
        try {
            endDate = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //计算会员结束时间与当前时间差 计算相差天数
        long days=(endDate.getTime()-nowDate.getTime())/(1000*3600*24);
        //计算余数 若大于15天 再加一个月会员价格 反之加半个月价格
        long months = days / 30;
        long remain = days % 30;
        if(remain>15){
            d = (months + 1) * p;
        }else{
            d = months * p + 0.5 * p;
        }
        return d;
    }
}
